package com.example.userregist;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class RegisteredActivity extends AppCompatActivity {

    private EditText editText1;
    private EditText editText2;
    private Button buttonregistered;
    private Button buttoncancel;
    private UserDatabaseHelper userDatabaseHelper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_registered);

        editText1 = findViewById(R.id.name1);
        editText2 = findViewById(R.id.password1);
        buttonregistered = findViewById(R.id.buttonregistered);
        buttoncancel = findViewById(R.id.buttoncancel);

        userDatabaseHelper = new UserDatabaseHelper(this);

        buttonregistered.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (userDatabaseHelper.queryforLogin(editText1.getText().toString(), editText2.getText().toString())){
                    Toast.makeText(RegisteredActivity.this,"已注册！",Toast.LENGTH_SHORT).show();
                }
                else{
                    userDatabaseHelper.insert(editText1.getText().toString(),editText2.getText().toString());
                    Toast.makeText(RegisteredActivity.this,"注册成功！",Toast.LENGTH_SHORT).show();
                }
            }
        });
        buttoncancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //1.创建意图对象
                Intent intent = new Intent(RegisteredActivity.this,MainActivity.class);
                //2.开启意图
                startActivity(intent);
            }
        });
    }
}
